//
//  LRDialog.m
//

#import "LRDialog.h"

@interface LRDialog()
@property (weak, nonatomic) IBOutlet UILabel *labTitle;
@property (weak, nonatomic) IBOutlet UILabel *labMessage;
@property (weak, nonatomic) IBOutlet UIButton *btnCancel;
@property (weak, nonatomic) IBOutlet UIButton *btnOk;

@property(nonatomic,copy) NSString *title;
@property(nonatomic,copy) NSString *message;
@property(nonatomic,copy) NSString *btnTitle;
@property(nonatomic,copy) NSString *btnCancelTitle;
@property(nonatomic,weak) UIView *bgView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@end

@implementation LRDialog


-(void)awakeFromNib{
    [super awakeFromNib];
    self.labMessage.font = [UIFont systemFontOfSize:14.f];

    self.bottomView.layer.cornerRadius = 6.f;
    self.bottomView.layer.masksToBounds = YES;
    
}

-(instancetype)initTitle:(NSString*)title message:(NSString*)message btnTitle:(NSString*)btnTitle btnCancelTitle:(NSString*)btnCancelTitle{
    self = [super init];
    if(self){
        self = [[NSBundle mainBundle] loadNibNamed:@"LRDialog" owner:self options:nil].firstObject;
        self.title = title;
        self.message = message;
        self.btnTitle = btnTitle;
        self.btnCancelTitle = btnCancelTitle;
        
        _labTitle.text = title;
        _labMessage.text = message;
        
        if(self.btnTitle!=nil && !ISEmpty(self.btnTitle)){
            self.btnOk.hidden = NO;
            [self.btnOk setTitle:self.btnTitle forState:UIControlStateNormal];
        }else{
            self.btnOk.hidden = YES;
        }
        
        if(self.btnCancelTitle!=nil && !ISEmpty(self.btnCancelTitle)){
            self.btnCancel.hidden = NO;
            [self.btnCancel setTitle:self.btnCancelTitle forState:UIControlStateNormal];
        }else{
            self.btnCancel.hidden = YES;
        }
        
    }
    return self;
}

- (IBAction)onCancel:(id)sender {
    WS(weakSelf);
    [UIView animateWithDuration:0.1 animations:^{
        self.bgView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.bgView removeFromSuperview];
        [self removeFromSuperview];
        if(weakSelf.cancelBlock){
            weakSelf.cancelBlock();
        }
    }];
}

- (IBAction)onMenu:(id)sender {
    WS(weakSelf);
    [UIView animateWithDuration:0.1 animations:^{
        weakSelf.bgView.alpha = 0;
        self.transform = CGAffineTransformMakeScale(0, 0);
    } completion:^(BOOL finished) {
        [weakSelf.bgView removeFromSuperview];
        [weakSelf removeFromSuperview];
        if(weakSelf.block){
            weakSelf.block();
        }
    }];
}

+(instancetype)dialog:(NSString*)title message:(NSString*)message btnTitle:(NSString*)btnTitle btnCancelTitle:(NSString*)btnCancelTitle{
    return [[self alloc]initTitle:title message:message btnTitle:btnTitle btnCancelTitle:btnCancelTitle];
}

//117
-(void)show{

    CGFloat width = SCRENNWIDTH - 80;
    self.width = width;
    CGFloat msgHeight = [self.message sizeWithFont:[UIFont systemFontOfSize:14.f] maxWidth:(width - 40.f)].height;
    
    self.height = 178.f+msgHeight;
    
    UIWindow *keyWind = [UIApplication sharedApplication].keyWindow;
    UIView *bgView = [UIView new];
    bgView.frame = CGRectMake(0, 0, SCRENNWIDTH, SCRENNHEIGHT);
    bgView.backgroundColor = color33;
    bgView.alpha = 0;
    [keyWind addSubview:bgView];
    self.bgView = bgView;
    self.center = keyWind.center;
    [keyWind addSubview:self];
    self.transform = CGAffineTransformMakeScale(0, 0);
    [UIView animateWithDuration:0.2 animations:^{
        bgView.alpha = 0.4;
        self.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        
    }];
}

- (IBAction)onClose:(id)sender {
    WS(weakSelf);
    [UIView animateWithDuration:0.1 animations:^{
       weakSelf.bgView.alpha = 0;
    } completion:^(BOOL finished) {
       [weakSelf.bgView removeFromSuperview];
       [weakSelf removeFromSuperview];
    }];
}


@end
